const Koa = require('koa')
const app = new Koa()
require('./db/model/constraint/index.js')

// 可以把 POST 请求的参数解析到ctx.request.body
const bodyparser = require('koa-bodyparser')
app.use(bodyparser({
    enableTypes: ['json', 'form', 'text']
}))

// 解决跨域
const cors = require('koa2-cors')
app.use(cors({
    maxAge: 5, //指定本次预检请求的有效期，单位为秒。
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
    credentials: true, // 允许跨域请求的情况下从其他域发送cookies
    allowHeaders: ['Content-Type', 'Authorization', 'Accept']
}))
// Routes
const KoaRouter = require('koa-router')
const router = new KoaRouter()
router.get(`/`, async (ctx) => {
    ctx.body = 'welcome to koa';
})
app.use(router.allowedMethods()).use(router.routes());
const example = require('./routes/example')
app.use(example.routes(), example.allowedMethods())
const file = require('./routes/file')
app.use(file.routes(), file.allowedMethods())

// ['*/*'] 代表所有文件类型将进行 Base64 编码
app.binaryTypes = ['*/*']

module.exports = app
